﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Text;
using System.Linq.Expressions;

namespace EttainGroup.Filters
{
    public class XmlFilterRepository : IFilterRepository
    {
        private XDocument doc = null;
        public XmlFilterRepository(string connectionString)
        {
            doc = XDocument.Load(connectionString);
            
            
        }
        public List<FilterDefinition> FiltersForExpression(string typeName, string componentName, string groupId)
        {
            var filters = from fs in doc.Descendants("Filter")
                          where fs.Element("Group").Value.Equals(groupId) && 
                          fs.Element("Entity").Value.Equals(typeName) &&
                          fs.Element("Component").Value.Equals(componentName)
                          select new FilterDefinition()
                          {
                              ComparisonType = (FilterComparisonType)Enum.Parse(typeof(FilterComparisonType), fs.Element("ComparisonType").Value),
                              ValueType = (FilterValueType)Enum.Parse(typeof(FilterValueType), fs.Element("Type").Value),
                              PropertyName = fs.Element("Property").Value,
                              EntityName = fs.Element("Entity").Value,
                              Group = fs.Element("Group").Value,
                              Values = (from v in fs.Descendants("Value")
                                       select new FilterDefinitionValue() { 
                                        Value = v.Element("Primary").Value,
                                        Secondary = v.Element("Secondary") == null ? null : v.Element("Secondary").Value
                                       }).ToList()
                          };
            return filters.ToList();
        }

        public Expression<Func<T, bool>> BuildPredicate<T>(Expression<Func<T, bool>> additionalFilter, string componentName, string groupId)
        {
            return Utility.BuildPredicate<T>(this, additionalFilter, componentName, groupId);
        }

        private List<FilterGroupDefinition> GetExpressionGroupFilter(string componentName, string externalEntityId){
            var filters = (from fg in doc.Descendants("FilterGroup")
                           where fg.Element("ComponentName").Value.Equals(componentName) &&
                           fg.Element("EntityId").Value.Equals(externalEntityId)
                           select new FilterGroupDefinition()
                           {
                               ComponentName = fg.Element("ComponentName").Value,
                               EntityId = Convert.ToInt32(fg.Element("EntityId").Value),
                               GroupId = Convert.ToInt32(fg.Element("Id").Value),
                               Filters = (
                             from f in fg.Descendants("Filter")
                             select new FilterDefinition()
                             {
                                 ComparisonType = (FilterComparisonType)Enum.Parse(typeof(FilterComparisonType), f.Element("ComparisonType").Value),
                                 ValueType = (FilterValueType)Enum.Parse(typeof(FilterValueType), f.Element("Type").Value),
                                 PropertyName = f.Element("Property").Value,
                                 EntityName = f.Element("Entity").Value,
                                 Group = f.Element("Group").Value,
                                 Values = (from v in f.Descendants("Value")
                                           select new FilterDefinitionValue()
                                           {
                                               Value = v.Element("Primary").Value,
                                               Secondary = v.Element("Secondary") == null ? null : v.Element("Secondary").Value
                                           }).ToList()
                             }).ToList()
                           }).ToList();
            return filters;
        }

        public List<System.Linq.Expressions.Expression<Func<T, bool>>> GetExpressionListForGroup<T>(System.Linq.Expressions.Expression<Func<T, bool>> additionalFilter, string componentName, string externalEntityId)
        {
            List<System.Linq.Expressions.Expression<Func<T, bool>>> expressions = new List<System.Linq.Expressions.Expression<Func<T, bool>>>();
            return expressions;
        }
    }
}
